var WxParse = require('../../../wxParse/wxParse.js')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.globalData.imgurl,
    selectorHidden: true, //隐藏底部选项,
    dotsClass:['on','','',''],
    imgs: [{
      picUrl: '/img/goodimg_03.png'
    }, {
      picUrl: '/img/goodimg_03.png'
    }, {
      picUrl: '/img/goodimg_03.png'
    }, {
      picUrl: '/img/goodimg_03.png'
    }],
    nums:0,
    days:0,
    selectorHidden:true, // 规格列表
    isShowpl:true,  //评价
    goodtype:0,
    selectedId:'' // 规格
  },
  onLoad:function(e){
    this.getDetail(e)
    this.getGuige(e)
    this.setData({
      para:e
    })
  },
  // banner轮播切换事件
  swiperChange: function (e) {
    var current = e.detail.current;
    var dotsClass = this.data.dotsClass; // banner 点
    for (var i = 0; i < dotsClass.length; i++) {
      dotsClass[i] = ''
    }
    dotsClass[current] = 'on';
    this.setData({
      dotsClass: dotsClass
    })
  },
  getDetail:function(e){
    console.log(e)
    var that = this
    wx.request({
      url: app.globalData.url + 'product_xq?id=' + e.id + '&openid=' + wx.getStorageSync('openId'),
      success:function(res){
        console.log(res)
        var gooddetail = res.data[0]
        var article = gooddetail.content;
        WxParse.wxParse('article', 'html', article, that, 5);
        that.setData({
          gooddetail: gooddetail,
          cuxiao_price: gooddetail.cuxiao_price,
          price: gooddetail.price,
          yajin: gooddetail.yajin,
        })
      }
    })
  },
  // 获取规格
  getGuige:function(e){
    var that = this
    wx.request({
      url: app.globalData.url + 'product_gg?pid=' + e.id,
      success: function (res) {
        var goodGG = res.data
        that.setData({
          goodGG: goodGG
        })
      }
    })
  },
  onText:function(e){
    var o = e.currentTarget.dataset
    console.log(o)
    this.setData({
      selectedGG:o,
      guivalue: o.guivalue,
      cuxiao_price: o.cuprice,
      price: o.price,
      yajin: o.yajin,
      selectedId: o.id // 规格
    })
  },
  // 减少租赁时长
  daysMinus: function () {
    var days = this.data.days
    if (days > 0) {
      days--;
    }
    this.setData({
      days: days
    })
  },
  // 增加租赁时长
 daysPlus: function () {
   var days = this.data.days
    days++;
    this.setData({
      days: days
    })
  },
  // 减少商品数量
  bindMinus:function(){
    var nums = this.data.nums
      if (nums>0){
        nums--;
      }
    this.setData({
      nums: nums
    })
  },
  // 增加商品数量
  bindPlus:function(){
    var nums = this.data.nums
      nums++;
    this.setData({
      nums: nums
    })
  },
  onShow: function () {
  },
  // 加入购物车
  onSubmit:function(e){
    var that = this
    var data = {
      shid: that.data.gooddetail.sh_id,
      pid: that.data.gooddetail.id,
      openid: wx.getStorageSync('openId'),
      number: that.data.nums,
      type: that.data.goodtype,
      unit: that.data.days,
      pzid: that.data.selectedId, // 规格
    }
    if(data.number == 0){
      wx.showModal({
        title: '提示',
        content: '请选择租赁数量',
        showCancel: false,
      })
    } else if (data.pzid == '') {
      wx.showModal({
        title: '提示',
        content: '请选择商品规格',
        showCancel: false,
      })
    } else if(data.unit == 0){
      wx.showModal({
        title: '提示',
        content: '请选择租赁时间',
        showCancel: false,
      })
    }else{
      wx.request({
        url: app.globalData.url + 'shopping_add',
        data: data,
        success: function (res) {
          console.log(res)
          wx.showToast({
            title: '已加入购物车',
            icon: 'success',
            duration: 2000,
            mask: true,
          })
        }
      })
    }
    
  },
  // 立即购买
  toBuy:function(e){
    var that = this
    var data = {
      shid: that.data.gooddetail.sh_id,
      pid: that.data.gooddetail.id,
      openid: wx.getStorageSync('openId'),
      number: that.data.nums,
      type: that.data.goodtype,
      unit: that.data.days,
      pzid: that.data.selectedId, // 规格
    }
    if (data.number == 0) {
      wx.showModal({
        title: '提示',
        content: '请选择租赁数量',
        showCancel: false,
      })
    } else if (data.pzid == '') {
      wx.showModal({
        title: '提示',
        content: '请选择商品规格',
        showCancel: false,
      })
    } else if (data.unit == 0) {
      wx.showModal({
        title: '提示',
        content: '请选择租赁时间',
        showCancel: false,
      })
    } else {
      wx.request({
        url: app.globalData.url + 'shopping_add_type?zhuangtai=1',
        data: data,
        success: function (res) {
          console.log(res)
          wx.setStorage({
            key: 'ids',
            data: res.data,
            success:()=>{
              wx.navigateTo({
                url: '/pages/payment/zulin',
              })
            }
          })
        }
      })
    }
  },
  // 回到首页
  backHome: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  // 购物车
  toCart:function(){
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
  //收藏
  collect:function(e){
    var that = this
    var o = e.currentTarget.dataset;
    if(o.shoucang==0){
      var para = 'collection_add?'
    }else{
      var para = 'collection_del?'
    }
    wx.request({
      url: app.globalData.url + para + 'pid=' + that.data.gooddetail.id + '&openid=' + wx.getStorageSync('openId') + '&type=0',
      success:function(res){
        console.log(res)
        that.getDetail(that.data.para)
        that.getGuige(that.data.para)
      }
    })
  }
})